package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;

import ejb.CommentInspectRemote;
import ejb.LikeFacadeEJBRemote;
import ejb.TagFacadeEJBRemote;
import ejb.TargetFacadeEJBRemote;
import ejb.UserFacadeEJBRemote;

import model.Mycomment;
import model.Myuser;
import model.Target;

/**
 * Servlet implementation class AddComment
 */
@WebServlet("/AddComment")
public class AddComment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@PersistenceUnit
	EntityManagerFactory emf;

    @Resource private UserTransaction utx; 

	@PersistenceContext
	EntityManager em;   
	@EJB CommentInspectRemote cir;
	@EJB LikeFacadeEJBRemote lf;
    @EJB UserFacadeEJBRemote uf;
    @EJB TargetFacadeEJBRemote tf;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddComment() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("home.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String targetid=((String) request.getParameter("targetid"));
		String commentstr=((String) request.getParameter("commentstr"));
		String user1=(String) request.getParameter("user1");
		String user2=(String) request.getParameter("user2");
		PrintWriter out = response.getWriter();
		out.print(targetid+"  "+commentstr+user1+user2);
		Mycomment comment=new Mycomment();;
		try{
		HttpSession session=request.getSession();
		String username=(String) session.getAttribute("username");
		if(username!=null){
			
		Myuser u1=new Myuser();
		if(user1!=null&&user1!=""){
			u1=uf.findUserByName(user1);
		}
		Myuser u2=new Myuser();
		out.print(u2.getUsername());
		if(user2!=null&&!user2.equals("")){
		u2=uf.findUserByName(user2);
		}
		if(targetid!=null){
			int id=Integer.parseInt(targetid) ;
			Target target=tf.queryByTargetID(id);
			if(target!=null){
				comment.setTargetBean(target);
			}
			if(u1!=null){
				comment.setMyuser1(u1);
			}
			if(commentstr!=null){
				comment.setContent(commentstr);
			}
			if(u2!=null&&u2.getUsername()!=null &&!u2.getUsername().equals("")){
				comment.setMyuser2(u2);
			}
		}
		}
		else{
			response.sendRedirect("login.jsp");
		}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		try{
		if(comment!=null){
			cir.addComment(comment);
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		response.sendRedirect("TargetServlet?targetID="+targetid);
	}

}
